package com.sofly.controller.group3;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.sofly.entity.DataGrid;
import com.sofly.entity.Suppliers;
import com.sofly.service.group3.ISuppliersService;

/**
 * 供货商控制类
* <p>Title:SuppliersController </p>
* <p>Description: </p>
* <p>Company: </p> 
* @author ChengShiMin
* @date 下午2:37:18
 */
@Controller
public class SuppliersController {

	@Autowired
	private DataGrid dataGrid;
	@Autowired
	private ISuppliersService suppliersService;
	@Autowired
	private AbstractExcelView abstractExcelView;
	@RequestMapping("login2")
	public String Login(){
		return "Suppliers";
	}
	/**
	 * 供货商列表
	 * @param page
	 * @param rows
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/href/supplierslist")
	@ResponseBody
	public DataGrid getSuppliersList(String page,String rows) throws UnsupportedEncodingException{		
	   int page1 = Integer.parseInt(page);
	   int rows1 = Integer.parseInt(rows);
	   List<Suppliers> list = suppliersService.getSuppliersList((page1-1)*rows1,rows1);
	   int total = suppliersService.getCount();
	   dataGrid.setRows(list);
	   dataGrid.setTotal(total);
	   return dataGrid;
	}
	/**
	 * 添加供应商
	 * @param suppliers
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/href/addSuppliers")
	public int addSuppliers(Suppliers suppliers){
		
		int i = suppliersService.addSuppliers(suppliers);
		return i;
	}
	/**
	 * 生成供应商excel表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/href/excel")
	 public ModelAndView viewExcel() {
		
		Map<String, Object> model = new HashMap<>();
		
		model.put("suppliersList",suppliersService.getSupplierList());
		
		return new ModelAndView(abstractExcelView, model);
	}
	/**
	 * 供应商查询
	 * @param supplierId
	 * @return
	 */
	@RequestMapping(value="/href/SearchSupplier")
	@ResponseBody
	public Suppliers searchSupplier(String supplierId){
		
		Suppliers suppliers = suppliersService.getSuppliersBySupplierId(supplierId);
		
		return suppliers;
	}
	/**
	 * 删除供货商
	 * @param supplierId
	 * @return
	 */
	@RequestMapping(value="/href/deleteSupplier",method=RequestMethod.GET)
	@ResponseBody
	public int deleteSupplier(String supplierId){		 
		return suppliersService.removeSupplier(supplierId);
	}
	/**
	 * 修改供货商信息
	 * @param suppliers
	 * @return
	 */
	@RequestMapping(value="/href/editSuppliers",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public int editSupplier(Suppliers suppliers){
		int i = suppliersService.editSupplier(suppliers);
		return  i;
	}
}
